package com.eastedu.service;

import com.eastedu.common.enums.DataSourceTypeEnum;
import com.eastedu.model.RequestParameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 〈〉
 *
 * @author ZHISHUAI.ZHANG
 * @since 2021/7/29
 */
@Slf4j
@Service
public class DataSourceManageServiceImpl implements DataSourceManageService {

    private final Map<String, DataSourceService> dataSources;

    public DataSourceManageServiceImpl(Map<String, DataSourceService> dataSources) {
        this.dataSources = dataSources;
    }

    @Override
    public void process(List<DataSourceTypeEnum> types) {
        types.forEach(dataSourceType -> {
            final DataSourceService dataSourceService = this.dataSources.get(dataSourceType.getName());
            if (Objects.isNull(dataSourceService)) {
                log.warn("不支持的数据源 dataSourceType：{}", dataSourceType);
                return;
            }
            dataSourceService.sync(new RequestParameter());
        });
    }

    @Override
    public void runScript(List<DataSourceTypeEnum> types, String stage) {
        types.forEach(dataSourceType -> {
            final DataSourceService dataSourceService = this.dataSources.get(dataSourceType.getName());
            if (Objects.isNull(dataSourceService)) {
                log.warn("不支持的数据源 dataSourceType：{}", dataSourceType);
                return;
            }
            dataSourceService.runScript(stage);
        });
    }
}